package com.xian.im.modules.command.system;


import com.xian.im.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: xian-im-server
 * @description:
 * @author: liru.xian
 * @create: 2021-01-27 09:26
 **/
@Slf4j
public class SystemHandlerCommandFactory {

    private static Map<String, SystemCommand> map = new ConcurrentHashMap<>();
    static{
        ServiceLoader<SystemCommand> loader = ServiceLoader.load(SystemCommand.class);
        for (SystemCommand command : loader) {
            map.put(command.cmdCode(),command);
        }
    }

    public static SystemCommand type(String cmdCode) {
        SystemCommand command = map.get(cmdCode);
        if (command != null) {
            return command;
        }
        throw new BusinessException("参数错误");
    }
}
